package com.material.vtool.service.impl;

import com.material.vtool.entity.BUserShareMaterialLogEntity;
import com.material.vtool.entity.respEntity.ReturnCode;
import com.material.vtool.exception.ServiceException;
import com.material.vtool.mapper.*;
import com.material.vtool.service.UserShareMaterialLogService;
import com.material.vtool.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.locks.ReentrantLock;

@Service
public class UserShareMaterialLogServiceImpl implements UserShareMaterialLogService {
    private final ReentrantLock materialLock = new ReentrantLock();

    private final ReentrantLock userLock = new ReentrantLock();

    private final ReentrantLock teamMaterialLock = new ReentrantLock();

    private final ReentrantLock teamRelationLock = new ReentrantLock();

    private final ReentrantLock teamInfoLock = new ReentrantLock();

    @Autowired
    private UserShareMaterialLogMapper userShareMaterialLogMapper;

    @Autowired
    private MaterialInfoMapper materialInfoMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private TeamMaterialMapper teamMaterialMapper;

    @Autowired
    private TeamRelationMapper teamRelationMapper;

    @Autowired
    private TeamInfoMapper teamInfoMapper;

    /**
     * 保存文章分享日志（异步执行）
     * @param userId 分享人ID
     * @param materialId 文章ID
     * @return
     */
    @Override
    @Transactional
    @Async
    public Integer addUserMaterialShare(Integer userId, Integer materialId) {
        BUserShareMaterialLogEntity entity = new BUserShareMaterialLogEntity();
        entity.setUserId(userId);
        entity.setMaterialId(materialId);
        entity.setCreateTime(DateUtil.getCurDate());
        Integer affectedCount = userShareMaterialLogMapper.insertUserShareMaterialLog(entity);
        if (affectedCount == null || affectedCount.intValue() == 0) throw new ServiceException("保存失败", ReturnCode.SERVER_ERROR);

        // 文章分享数量 + 1
        try {
            materialLock.lock();
            Integer affectedCount1 = materialInfoMapper.increaseShareCount(materialId);
            if (affectedCount1 == null || affectedCount1.intValue() == 0) throw new ServiceException("保存失败", ReturnCode.SERVER_ERROR);
        } finally {
            materialLock.unlock();
        }

        // 用户分享数量 + 1
        try {
            userLock.lock();
            Integer affectedCount2 = userInfoMapper.increaseShareCount(userId);
            if (affectedCount2 == null || affectedCount2.intValue() == 0) throw new ServiceException("保存失败", ReturnCode.SERVER_ERROR);
        } finally {
            userLock.unlock();
        }

        return affectedCount;
    }

    /**
     * 保存团队文章分享日志（异步执行）
     * @param userId 用户ID
     * @param materialId 素材ID
     * @param teamId 团队ID
     * @return
     */
    @Override
    @Transactional
    @Async
    public Integer addUserTeamMaterialShare(Integer userId, Integer materialId, Integer teamId) {
        BUserShareMaterialLogEntity entity = new BUserShareMaterialLogEntity();
        entity.setUserId(userId);
        entity.setMaterialId(materialId);
        entity.setTeamId(teamId);
        entity.setCreateTime(DateUtil.getCurDate());
        Integer affectedCount = userShareMaterialLogMapper.insertUserShareMaterialLog(entity);
        if (affectedCount == null || affectedCount.intValue() == 0) throw new ServiceException("保存失败", ReturnCode.SERVER_ERROR);

        // 团队素材的分享次数 +1
        try {
            teamMaterialLock.lock();
            Integer affectedCount1 = teamMaterialMapper.increaseShareCount(materialId);
            if (affectedCount1 == null || affectedCount1.intValue() == 0)  throw new ServiceException("保存失败", ReturnCode.SERVER_ERROR);
        } finally {
            teamMaterialLock.unlock();
        }

        //  团队成员的分享次数 +1
        try {
            teamRelationLock.lock();
            Integer affectedCount2 = teamRelationMapper.increaseShareCount(teamId, userId);
            if (affectedCount2 == null || affectedCount2.intValue() == 0)  throw new ServiceException("保存失败", ReturnCode.SERVER_ERROR);
        } finally {
            teamRelationLock.unlock();
        }

        // 团队的分享次数 +1
        try {
            teamInfoLock.lock();
            Integer affectedCount3 = teamInfoMapper.increaseShareCount(teamId);
            if (affectedCount3 == null || affectedCount3.intValue() == 0)  throw new ServiceException("保存失败", ReturnCode.SERVER_ERROR);
        } finally {
            teamInfoLock.unlock();
        }

        // 文章分享数量 + 1
        try {
            materialLock.lock();
            Integer affectedCount4 = materialInfoMapper.increaseShareCount(materialId);
            if (affectedCount4 == null || affectedCount4.intValue() == 0) throw new ServiceException("保存失败", ReturnCode.SERVER_ERROR);
        } finally {
            materialLock.unlock();
        }

        // 用户分享数量 + 1
        try {
            userLock.lock();
            Integer affectedCount5 = userInfoMapper.increaseShareCount(userId);
            if (affectedCount5 == null || affectedCount5.intValue() == 0) throw new ServiceException("保存失败", ReturnCode.SERVER_ERROR);
        } finally {
            userLock.unlock();
        }

        return affectedCount;
    }
}
